package com.wolf.etl.core.rule.check;

import com.wolf.etl.core.ETLManager;
import com.wolf.etl.enumeration.CHECK_RULE_TYPE;
import com.wolf.vo.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by tommy's father on 2017/8/17.
 */
@Transactional
public abstract class AbstractCheckRule {

    private static Logger logger = LoggerFactory.getLogger(AbstractCheckRule.class);

    private static final String logger_prefix = "wolf-etl#com.wolf.etl.core.rule.check#AbstractCheckRule";


//    public abstract BaseResult check(String cxt, String srcColumnName, String targetColumnName);

    public abstract BaseResult check(String cxt, String value);


    private static final Object lock = new Object();

    private ETLManager manager;

    public  static AbstractCheckRule getInstance(CHECK_RULE_TYPE ruleType, ETLManager manager) {
        synchronized (lock) {
            try {
                Class<?> clazz = Class.forName(String.format("%s.%s",getPackageName(), ruleType.getCheckRuleType()));
                AbstractCheckRule abstractCheckRule = (AbstractCheckRule) manager.getBeanManager().createBean(clazz);
                abstractCheckRule.manager = manager;
                return abstractCheckRule;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static final String getPackageName(){
        return new Object(){
            public String getClassForStatic(){
                return this.getClass().getPackage().getName();
            }
        }.getClassForStatic();
    }
}
